package com.jueecy.controller;

import com.jueecy.pojo.Goods;
import com.jueecy.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    //index页面,商品列表
    @GetMapping("/goods_details")
    public String goods_details(){
        return "goods_details";
    }

    //想买—发布商品详情页
    @GetMapping("/want_sell")
    public String want_sell(){
        return "want_sell";
    }

    //想买—发布商品(添加想买商品到数据库)
    @ResponseBody
    @RequestMapping("/add_want_sell")
    public Boolean add_want_sell(String goods_name,
                                 String goods_describe,
                                 Integer goods_type,
                                 Double goods_price,
                                 Integer user_id,
                                 String goods_picture){

//      把字符串中的反斜杠（/）替换成（\）
        goods_picture = goods_picture.replaceAll("\\\\", "/");
        goods_picture = goods_picture.replace("D:/static", "");
        Goods goods=new Goods();
        goods.setGoods_name(goods_name);
        goods.setGoods_describe(goods_describe);
        goods.setGoods_type(goods_type);
        goods.setGoods_price(goods_price);
        goods.setUser_id(user_id);
        goods.setGoods_picture(goods_picture);

        Integer tmp = goodsService.addGoods(goods);
        if (tmp==1){
            return true;
        }else {
            return false;
        }
    }


    //查询所有商品
    @ResponseBody
    @GetMapping("/queryAllGoods")
    public List queryAllGoods(){
        List goodsList=goodsService.queryAllGoods();
        return goodsList;
    }


    //查询用户购物车内的所有商品
    @ResponseBody
    @GetMapping("/queryShoppingCart")
    public List queryShoppingCart(Integer user_id){
        List goodsList=goodsService.queryShoppingCart(user_id);
        return goodsList;
    }


    //添加商品到购物车
    @ResponseBody
    @GetMapping("/addToShoppingCart")
    public Boolean addToShoppingCart(Integer user_id,Integer goods_id){
        Integer tmp = goodsService.addToShoppingCart(user_id,goods_id);
        if (tmp==1){
            return true;
        }else {
            return false;
        }
    }


    //从购物车中删除某件商品
    @ResponseBody
    @GetMapping("/deleteFromShoppingCart")
    public Boolean deleteFromShoppingCart(Integer user_id,Integer goods_id){
        Integer tmp = goodsService.deleteFromShoppingCart(user_id,goods_id);
        if (tmp==1){
            return true;
        }else {
            return false;
        }
    }


    //按照左侧导航查询不同类型的商品
    @ResponseBody
    @GetMapping("/findGoodsByType")
    public List<Goods> findGoodsByType(String goods_type){
        Integer flag=null;
        switch (goods_type){
            case "电器": flag=1; break;
            case "数码": flag=2; break;
            case "鞋服": flag=3; break;
            case "美妆": flag=4; break;
            case "食品": flag=5; break;
            case "运动": flag=6; break;
            case "图书": flag=7; break;
            case "其他": flag=8; break;
            default:System.out.println("变换完成");
        }
        List<Goods> goodsList=goodsService.findGoodsByType(flag);
        return goodsList;
    }



    //按照关键字查找商品
    @ResponseBody
    @GetMapping("/searchGoodsByName")
    public List<Goods> searchGoodsByName(String keywords){
        List<Goods> goodsList=goodsService.searchGoodsByName(keywords);
        return goodsList;
    }


    //用户已经发布的所有商品
    @ResponseBody
    @GetMapping("/alreadySellGoods")
    public List<Goods> alreadySellGoods(Integer user_id){
        List<Goods> goodsList=goodsService.alreadySell(user_id);
        return goodsList;
    }


    //删除已经发布的某件商品
    @ResponseBody
    @GetMapping("/deleteMyGoods")
    public Boolean deleteMyGoods(Integer user_id,Integer goods_id){
        Integer tmp = goodsService.deleteMyGoods(user_id,goods_id);
        if (tmp==1){
            return true;
        }else {
            return false;
        }
    }



}
